package com.weixing.mall.provider.support.excel.act;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.api.model.query.ActGoodsQuery;
import com.weixing.mall.provider.api.model.vo.ActivityProductVo;
import com.weixing.mall.provider.api.service.ItemFeign;
import com.weixing.mall.provider.model.domain.Activity;
import com.weixing.mall.provider.service.IActivityGoodsService;
import com.weixing.mall.util.SKUUtils;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动商品导入模版读取类
 *
 * @author Small
 */
public class ActGoodsImportDataListener extends AnalysisEventListener<ActGoodsImportData> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ActGoodsImportDataListener.class);
    /**
     * 每隔500条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 500;
    List<ActGoodsImportData> list = new ArrayList<ActGoodsImportData>();
    @Autowired
    private IActivityGoodsService activityGoodsService;
    @Autowired
    private ItemFeign itemFeign;

    private  Long actId;

    /**
     * 活动id
    */
    private Activity activity;

    public ActGoodsImportDataListener() {
    }

    @Autowired
    public ActGoodsImportDataListener(IActivityGoodsService activityGoodsService, ItemFeign itemFeign,Long actId) {
        this.activityGoodsService = activityGoodsService;
        this.itemFeign = itemFeign;
        this.actId=actId;
    }

    @SneakyThrows
    @Override
    public void invoke(ActGoodsImportData data, AnalysisContext context) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
        list.add(data);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @SneakyThrows
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        LOGGER.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() throws Exception {
        LOGGER.info("{}条数据，开始存储数据库！", list.size());
        Map<String, String> map = new HashMap<String, String>();
        List<ActivityProductVo> listResult = Lists.newArrayList();
        for (ActGoodsImportData data : list) {
            ActGoodsQuery goodsQuery = new ActGoodsQuery();
            goodsQuery.setGoodsSn(data.getGoodsn());
            List<ActivityProductVo> tmp = itemFeign.findByGoodsn(data.getGoodsn()).getData();
            for (ActivityProductVo vo : tmp) {
                  String color= SKUUtils.color(vo.getProductSkuDesp());
                  String  size=SKUUtils.size(vo.getProductSkuDesp());
                if (color.equals(data.getColor()) && size.equals(data.getZize())) {
                    vo.setProductStock(data.getNum());
                    listResult.add(vo);
                }
            }
        }
        activityGoodsService.importActGoods(listResult,actId);
        LOGGER.info("存储数据库成功！");
    }

}
